package com.codemes.happylist.utils;

import com.codemes.happylist.modules.music.MusicFile;
import javafx.scene.media.Media;
import lombok.extern.slf4j.Slf4j;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author nscoffee
 */
@Slf4j
public class MediaUtil {

    private static final String GB2312 = "GB2312";

    public static MusicFile extract(File file){
        MusicFile musicFile = new MusicFile();
        musicFile.setAbsPath(file.getAbsolutePath());
        musicFile.setTitle(file.getName());
        try {
            MP3File mp3File = new MP3File(file.getAbsoluteFile());

            AbstractID3v2Tag id3v2tag=  mp3File.getID3v2Tag();
            if(null==id3v2tag){
                log.warn("无法获取音频信息 -> {}",file.getAbsolutePath());
                return musicFile;
            }
            String album = new String(id3v2tag.getFirst(FieldKey.ALBUM).getBytes(StandardCharsets.ISO_8859_1),GB2312);
            musicFile.setAlbum(album);
            String singer = new String(id3v2tag.getFirst(FieldKey.ARTIST).getBytes(StandardCharsets.ISO_8859_1),GB2312);
            musicFile.setSinger(singer);
            String title =  new String(id3v2tag.getFirst(FieldKey.TITLE).getBytes(StandardCharsets.ISO_8859_1),GB2312);
            if(!ObjectUtils.isEmpty(title)){
                musicFile.setTitle(title);
            }

            MP3AudioHeader header = mp3File.getMP3AudioHeader();
            musicFile.setDuration(header.getTrackLength());
        } catch (IOException | InvalidAudioFrameException | TagException | ReadOnlyFileException | CannotReadException e) {
            log.error("extract media info occur error -> {]",e);
        }
        return musicFile;
    }

    public static void main(String[] args) throws IOException, TagException, CannotReadException, InvalidAudioFrameException, ReadOnlyFileException {
        File file = new File("J:\\LS_音乐\\范玮琪\\范玮琪 - 可不可以不勇敢.mp3");
        System.out.println(file.exists());
        System.out.println(file.toURI().toURL().toExternalForm());

        MP3File mp3File = new MP3File(file.getAbsoluteFile());
        MP3AudioHeader header = mp3File.getMP3AudioHeader();
        System.out.println("时长: " + header.getTrackLength()); //获得时长
        System.out.println("比特率: " + header.getBitRate()); //获得比特率
        System.out.println("音轨长度: " + header.getTrackLength()); //音轨长度
        System.out.println("格式: " + header.getFormat()); //格式，例 MPEG-1
        System.out.println("声道: " + header.getChannels()); //声道
        System.out.println("采样率: " + header.getSampleRate()); //采样率
        System.out.println("MPEG: " + header.getMpegLayer()); //MPEG
        System.out.println("MP3起始字节: " + header.getMp3StartByte()); //MP3起始字节
        System.out.println("精确的音轨长度: " + header.getPreciseTrackLength()); //精确的音轨长度

        System.out.println("----------------Loading...Content-----------------");
        AbstractID3v2Tag id3v2tag=  mp3File.getID3v2Tag();
        for (FieldKey fieldKey : FieldKey.values()) {
            String info = new String(id3v2tag.getFirst(fieldKey).getBytes(StandardCharsets.ISO_8859_1),GB2312);
            System.out.println(fieldKey.name()+"  "+ info);
        }

        System.out.println("-------------------media---------------------------");
        Media media = new Media(file.toURI().toURL().toExternalForm());
        System.out.println(media.getDuration());
        System.out.println(media.getHeight());
        System.out.println(media.getWidth());
        System.out.println(media.getMarkers());
        System.out.println(media.getMetadata());
        System.out.println(media.getSource());
        System.out.println(media.getTracks());
    }
}
